package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Tag(name="员工信息接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @Operation(summary = "用户退出", description = "用户退出")
    public Result<String> logout() {
        return Result.success();
    }


    @PostMapping
    @Operation(summary = "用户注册", description = "用户注册")
    public Result<String> save(@RequestBody EmployeeDTO employee) {
        log.info("员工注册：{}", employee);
        employeeService.save(employee);
        return Result.success();
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询用户列表", description = "分页查询用户列表")
    public Result<PageResult> pageList(EmployeePageQueryDTO employeeDTO) {
        log.info("用户查询条件：{}", employeeDTO);
        PageResult pageResult = employeeService.pageQuery(employeeDTO);
        return  Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "修改用户状态", description = "修改用户状态")
    public Result<String> setStartOrStop(@PathVariable Integer status, Long id) {
        log.info("用户修改状态, {}", status);
        employeeService.setStartOrStop(status, id);
        return Result.success("");
    }


    @GetMapping("/{id}")
    @Operation(summary = "获取用户的数据", description = "获取用户的数据")
    public Result<Employee> getId(@PathVariable Long id) {
        log.info("用户的id, {}", id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping
    @Operation(summary = "用户需要修改的数据", description = "用户需要修改的数据")
    public Result<Employee> update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("用户需要修改的数据, {}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
